#include "overviewchart.h"
#include "ui_overviewchart.h"

OverviewChart::OverviewChart(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::OverviewChart)
{
    ui->setupUi(this);
    this->setWindowTitle("数据统计");

    // 设置关闭窗口后不退出程序
    setAttribute(Qt::WA_QuitOnClose, false);
    setAttribute(Qt::WA_DeleteOnClose,true);

    initCombox();

    DtimePair dtpair;
    ComboxDateTime(dtpair);
    drawPie(dtpair);

}

OverviewChart::~OverviewChart()
{
    delete ui;
//    delete this;
}

void OverviewChart::initCombox()
{
    ui->daysRangeCB->addItem("今日");
    ui->daysRangeCB->addItem("昨日");
    ui->daysRangeCB->addItem("近7日");
    ui->daysRangeCB->addItem("近30日");
    ui->daysRangeCB->addItem("近6个月");
    ui->daysRangeCB->addItem("近1年");
    ui->daysRangeCB->addItem("全部");




}

void OverviewChart::ComboxDateTime(DtimePair &dtpair)
{
    QString flag = ui->daysRangeCB->currentText();
    if (flag == "今日")
    {
        dtpair.second = QDateTime::currentDateTime();
        dtpair.first = QDateTime(QDate::currentDate(),QTime::fromString("00:00:00", "hh:mm:ss"));
        return ;
    }
    else if (flag == "昨日")
    {
        dtpair.second = QDateTime(QDate::currentDate(),QTime::fromString("00:00:00", "hh:mm:ss"));
        dtpair.first = dtpair.second.addDays(-1);
        return ;
    }
    else if (flag == "近7日")
    {
        dtpair.second = QDateTime::currentDateTime();
        dtpair.first = dtpair.second.addDays(-7);
        return ;
    }
    else if (flag == "近30日")
    {
        dtpair.second = QDateTime::currentDateTime();
        dtpair.first = dtpair.second.addMonths(-1);
        return ;
    }
    else if (flag == "近6个月")
    {
        dtpair.second = QDateTime::currentDateTime();
        dtpair.first = dtpair.second.addMonths(-6);
        return ;
    }
    else if (flag == "近1年")
    {
        dtpair.second = QDateTime::currentDateTime();
        dtpair.first = dtpair.second.addYears(-1);
        return ;
    }
    else
    {
        dtpair.second = QDateTime::currentDateTime();
        dtpair.first = QDateTime::fromString("2022-01-01 00:00:00","yyyy-MM-dd hh:mm:ss");
        return ;
    }


}

void OverviewChart::loadData(const QDateTime &startDatetime, const QDateTime &endDatetime)
{
    for (int sort=0; sort <= 4; ++sort)
    {
        QString sql = "select taskname, sum(costTime) as second from tasklog where sort = :sort "
                "and datetime between :startDateTime and :endDateTime "
                "group by taskname;";

        Ndb::query.prepare(sql);
        Ndb::query.bindValue(":sort", sort);
        Ndb::query.bindValue(":startDateTime", startDatetime.toString("yyyy-MM-dd hh:mm:ss"));
        Ndb::query.bindValue(":endDateTime", endDatetime.toString("yyyy-MM-dd hh:mm:ss"));

        if (!Ndb::query.exec())
        {
            qDebug() << "tasklog sort=" << sort << " query error " << Ndb::query.lastError();
            continue;
        }

        QString name;
        int second;
        while(Ndb::query.next())
        {
            name = Ndb::query.value("taskname").toString();
            second = Ndb::query.value("second").toInt();
            m_data_map[sort].push_back(QPair<QString, int>(name, second/60));
        }
    }


}

void OverviewChart::drawPie(DtimePair &dtpair)
{
    for (int i=0; i <= 4; ++i)
        m_data_map.insert(i, QVector<QPair<QString, int>>());



    DrilldownChart *chart = new DrilldownChart();

    chart->setTheme(QChart::ChartThemeLight);
    chart->setAnimationOptions(QChart::AllAnimations);
    chart->legend()->setVisible(true);
    chart->legend()->setAlignment(Qt::AlignRight);

//    QDateTime now = QDateTime::currentDateTime();
//    loadData(now.addDays(-7),now);
    loadData(dtpair.first, dtpair.second);

    QPieSeries *yearSeries = new QPieSeries(this);
    yearSeries->setName(QString("%1总览").arg(ui->daysRangeCB->currentText()));


    QString sort_str;
    for (const int sort_num : m_data_map.keys()) {
        sort_str = taskItem::sortArray[sort_num];
        QPieSeries *series = new QPieSeries(this);
        series->setName(QString("%1任务总览").arg(sort_str));

        for (const auto &pair_ : m_data_map.value(sort_num))
            *series << new DrilldownSlice(pair_.second, pair_.first, yearSeries);

        QObject::connect(series, &QPieSeries::clicked, chart, &DrilldownChart::handleSliceClicked);

        *yearSeries << new DrilldownSlice(series->sum(), sort_str, series);
    }

    QObject::connect(yearSeries, &QPieSeries::clicked, chart, &DrilldownChart::handleSliceClicked);

    chart->changeSeries(yearSeries);


    ui->chart->setChart(chart);
}

void OverviewChart::selectComboxIndex(int index)
{
    ui->daysRangeCB->setCurrentIndex(index);
    on_daysRangeCB_currentIndexChanged(index);
}

void OverviewChart::on_daysRangeCB_currentIndexChanged(int index)
{
    DtimePair dtpair;
    ComboxDateTime(dtpair);
    drawPie(dtpair);
}
